
var spider = require("../utils/puppeteer-pool.js");
spider = new spider();
spider.init();
var filCache = require("../utils/file_cache.js");
var fs = require("fs");

const agent = async (req, res) => {
    var uri = req.originalUrl;
    uri = uri.replace("/?", "?");
    if (uri.endsWith("/")) {
        uri = uri.slice(0, -1);
    }

    if (uri.includes("readingpage")) {
        res.status(404).send("404 - 页面未找到");
        return;
    }

    //否则请求浏览器渲染后存入磁盘文件，返回
    const userAgent = req.headers['user-agent'];

    //存在磁盘文件那么返回
    let path = filCache.uriToPath(uri);
    let fileName = filCache.uriToFileName(uri);
    // 判断文件是否存在
    if (filCache.exists(path + fileName)) {
        console.error('命中缓存');
        fs.readFile(path + fileName, function (err, data) {
            if (err) throw err
            res.setHeader('Content-Type', 'text/html');
            res.send(data);
        })
    } else {
        //const ip = req.ip;
        const ip = "";
        var s = "";
        try {
            let url = "https://www.marliang.com.cn" + uri;
            console.log(new Date().toLocaleString() + " " + ip + " " + userAgent + " " + url);
            var s = await spider.htmlRender(url);
            res.send(s);
        } catch (e) {
            console.log(new Date().toLocaleString() + " " + e);
            return;
        }
        fs.mkdir(path, { recursive: true }, function (err) {
            fs.writeFile(path + fileName, s, err => {
                if (err) throw err
                console.log('文件已被写入' + path)
            });
        });

    }
}

module.exports = {
    agent
}